trigger TemplateInUse on Template__c (before delete) {
    if(Trigger.isDelete){
       for(Template__c t: Trigger.old){
          for(Batch__c b : [Select Status__c from Batch__c b where b.Template__c = :t.Id])
          {
              if(b.Status__c == 'Distributed')
                  t.addError('This template cannot be deleted as it has been used to generate Plan Documents.');          
          }
       }
   }
}